package Practice1;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class ContextServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        ServletContext context = getServletContext(); 与下行代码等价：
        ServletContext context = getServletConfig().getServletContext();
        System.out.println("username: " + context.getInitParameter("username"));
        System.out.println("password: " + context.getInitParameter("password"));
        System.out.println("当前工程相对路径：" + context.getContextPath());

        // “/”被服务器解析为：http://ip:port/工程名/
        System.out.println("当前工程绝对路径：" + context.getRealPath("/"));
        System.out.println("css文件夹的绝对路径：" + context.getRealPath("/css"));
        System.out.println("css文件夹的绝对路径：" + context.getRealPath("/css/abc.css"));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
